package com.hjq.controller;

import com.hjq.anno.LogOperation;
import com.hjq.pojo.PageResult;
import com.hjq.pojo.Result;
import com.hjq.pojo.Student;
import com.hjq.pojo.queryparam.StudentQueryParam;
import com.hjq.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生管理
 */
@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    StudentService studentService;
    /**
     * 学生列表分页查询
     * @param studentQueryParam  查询参数
     * @return 分页结果
     */
    @GetMapping
    public Result findByPage(StudentQueryParam studentQueryParam){
        log.info("查询参数：,{}",studentQueryParam);
        PageResult<Student> result = studentService.findByPage(studentQueryParam);
        return  Result.success(result);
    }

    /**
     * 新增学生
     * @param student  学生信息
     * @return 新增结果
     */
    @LogOperation
    @PostMapping
    public Result insert(@RequestBody Student student){
        log.info("新增学生：{}",student);
        studentService.insert(student);
        return Result.success();
    }

    /**
     * 根据id查询学生
     * @param id id
     * @return Student
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id ){
        log.info("根据id查询学生,{}",id);
        Student student = studentService.findById(id);
        return Result.success(student);
    }
    /**
     * 更新学生信息
     * @param student  学生信息
     * @return 更新结果
     */
    @LogOperation
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("更新学生信息,{}",student);
        studentService.update(student);
        return Result.success();
    }

    /**
     * 批量删除学生
     * @param ids 学生ID数组
     * @return 删除结果
     */
    @LogOperation
    @DeleteMapping("/{ids}")
    public Result deleteBatch(@PathVariable List<Integer> ids){
        log.info("批量删除学生信息：{}", ids);
        studentService.delete(ids);
        return Result.success();
    }
    /**
     * 扣分
     * @param id 学生ID
     * @param score 扣分分数
     * @return 扣分结果
     */
    @PutMapping("/violation/{id}/{score}")
    public Result handleViolation(@PathVariable Integer id, @PathVariable Integer score){
        log.info("处理违规，学生ID：{}, 扣分：{}", id, score);
        studentService.handleViolation(id, score);
        return Result.success();
    }
}
